package net.core.service.file;

import java.io.File;
import java.util.Date;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class UploadUtil {
	
	private static final int BUFFER_SIZE=16*1024;
	//以毫秒数来重新命名
	public static String saveNewNameFile(String filePath) {
        Date dt = new Date();
        long lg = dt.getTime();
        Long ld = new Long(lg);

        //初始化值
        String newName = ld.toString();

        int beginIndex = filePath.lastIndexOf("\\") + 1;
        int endIndex = filePath.lastIndexOf(".");
        String subStr = filePath.substring(beginIndex, endIndex);
        String newFilePath = filePath.replaceAll(subStr, newName);

        return newFilePath;
    }
	
    public static String subFileName(String newFilePath) {
        int a = newFilePath.lastIndexOf("\\") + 1;
        int b = newFilePath.length();
        return newFilePath.substring(a, b);
    }
    
    //截取文件后缀名
    public static String subFileType(String newFilePath){
    	int a=newFilePath.lastIndexOf(".")+1;
    	int b=newFilePath.length();
    	return newFilePath.substring(a,b); 
    }
    
    //原文件名
    public static String subOldFileName(String newFilePath){
    	int beginIndex = newFilePath.lastIndexOf("\\") + 1;
        int endIndex = newFilePath.lastIndexOf(".");
        return newFilePath.substring(beginIndex,endIndex);
    }
    
    //替换文件名
    public static String replaceFileName(String fileName,String newFileName){
		int index =fileName.lastIndexOf(".");
		String oldFileName = fileName.substring(0,index);
		return fileName.replace(oldFileName, newFileName);
    }
    
    //重命名文件名
    public static Boolean changeFileName(String oldFileName,String newFileName){
	     try{
		    File oldFile = new File(oldFileName.replace("\\", "/")); 
		    File newFile = new File(newFileName.replace("\\", "/"));
		    return oldFile.renameTo(newFile); 
	     }
	     catch(Exception e){
	    	 return false;
	     }
    }
    
    //截取序列号
    public static String getSbxlbh(String str){
    	int index = str.indexOf("-");
    	return str.substring(0,index);
    }
    /**
     * 复制文件
     * @param src
     * @param dst
     * @return
     */
	 public static boolean  copyfile(File src, File dst) {   
	        boolean result=false;   
	        InputStream in = null;   
	        OutputStream out = null;   
	        try {   
	            in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);   
	            out = new BufferedOutputStream(new FileOutputStream(dst),   
	                    BUFFER_SIZE);   
	            byte[] buffer = new byte[BUFFER_SIZE];   
	            int len = 0;   
	            while ((len = in.read(buffer)) > 0) {   
	                out.write(buffer, 0, len);   
	            }   
	            result=true;   
	        } catch (Exception e) {   
	            e.printStackTrace();   
	            result=false;   
	        } finally {   
	            if (null != in) {   
	                try {   
	                    in.close();   
	                } catch (IOException e) {   
	                    e.printStackTrace();   
	                }   
	            }   
	            if (null != out) {   
	                try {   
	                    out.close();   
	                } catch (IOException e) {   
	                    e.printStackTrace();   
	                }   
	            }   
	        }   
	        return result;   
	    }   

    
}
